package com.example.test.dialogfactory;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.PopupWindow;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;
import easier.recycler.BaseViewHolder;
import easier.recycler.MultiTypeAdapter;
import easier.recycler.TypeViewHolder;
import easier.window.Item;
import easier.window.popup.PopupWindowShow;
import easier.window.popup.recycler.PopupRecyclerEmpty;
import easier.window.popup.recycler.PopupRecyclerViewImpl01;
import easier.window.popup.recycler.PopupRecyclerViewImpl02;
import java.util.ArrayList;
import java.util.List;

public class MainActivity4 extends AppCompatActivity {

    public static void start( Context context ) {

        Intent starter = new Intent( context, MainActivity4.class );
        context.startActivity( starter );
    }

    @Override
    protected void onCreate( Bundle savedInstanceState ) {

        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main4 );

        View view = getLayoutInflater().inflate( R.layout.popup_item, null );

        PopupWindow popupWindow = new PopupWindow( getLayoutInflater().inflate( R.layout.popup_item, null ) );
        View click00 = findViewById( R.id.show );
        click00.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                PopupWindowShow.showAsDropDownWithAnchorWidth( popupWindow, v );
            }
        } );

        View click01 = findViewById( R.id.dismiss );
        click01.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                popupWindow.dismiss();
            }
        } );

        View click02 = findViewById( R.id.update );
        click02.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                TextView textView = popupWindow.getContentView().findViewById( R.id.text );
                textView.setText( "曾经沧海难为水\n除却巫山不是云" );

                PopupWindowShow.showAsDropDown( popupWindow, click00 );
            }
        } );

        View view2 = getLayoutInflater().inflate( R.layout.popup_item_recycler, null );
        RecyclerView recycler = view2.findViewById( R.id.recycler );
        recycler.setLayoutManager( new LinearLayoutManager( recycler.getContext() ) );
        recycler.setAdapter( new MultiTypeAdapter( convert( 4 ) ) );
        PopupWindow popupWindow1 = new PopupWindow( view2 );
        View click04 = findViewById( R.id.recyclerPop );
        click04.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                PopupWindowShow.showAsDropDownWithAnchorWidth( popupWindow1, v );
            }
        } );

        View view3 = getLayoutInflater().inflate( R.layout.popup_item_recycler, null );
        RecyclerView recyclerView = (RecyclerView) view3.findViewById( R.id.recycler );
        recyclerView.setLayoutManager( new LinearLayoutManager( recyclerView.getContext() ) );
        recyclerView.setAdapter( new MultiTypeAdapter( convert( 50 ) ) );
        PopupWindow popupWindow2 = new PopupWindow( view3 );
        View click05 = findViewById( R.id.recyclerPop2 );
        click05.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                PopupWindowShow.showAsDropDownWithAnchorWidth( popupWindow2, v );
            }
        } );

        View click06 = findViewById( R.id.recyclerPop3 );
        click06.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                PopupWindowShow.showAsDropDownWithAnchorWidth( popupWindow2, v );
            }
        } );

        View view1 = getLayoutInflater().inflate( R.layout.popup_choose_items, null );
        View view4 = getLayoutInflater().inflate( R.layout.popup_item_v2, null );

        //PopupFullDropDown popupView = new PopupFullDropDown( view1 );
        //BasePopupView popupView1 = new BasePopupView( view4 );

        PopupWindow popupWindow3 = new PopupWindow( view1 );
        View click07 = findViewById( R.id.recyclerPop4 );
        click07.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                PopupWindowShow.showAsDropDownDimBehind( popupWindow3, v );
            }
        } );

        PopupWindow popupWindow4 = new PopupWindow( view4 );
        view1.findViewById( R.id.text03 ).setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                PopupWindowShow.showAsDropDownWithAnchorWidth( popupWindow4, v, click07 );
            }
        } );

        findViewById( R.id.recyclerPop5 ).setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                List< Item > items = new ArrayList<>();
                items.add( new Item( "", "全部", false ) );
                items.add( new Item( "0", "待完善", false ) );
                items.add( new Item( "1", "审核中", false ) );
                items.add( new Item( "2", "审核通过", false ) );
                items.add( new Item( "3", "审核拒绝", false ) );
                items.add( new Item( "4", "待激活", false ) );

                PopupRecyclerViewImpl01 impl01 = new PopupRecyclerViewImpl01( v.getContext() );
                impl01.show( v, items );
            }
        } );

        findViewById( R.id.recyclerPop6 ).setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                List< Item > items = new ArrayList<>();

                for ( int i = 0; i < 5; i++ ) {

                    items.add( new Item( "", "全部", false ) );
                    items.add( new Item( "0", "待完善", false ) );
                    items.add( new Item( "1", "审核中", false ) );
                    items.add( new Item( "2", "审核通过", false ) );
                    items.add( new Item( "3", "审核拒绝", false ) );
                    items.add( new Item( "4", "待激活", false ) );

                }

                PopupRecyclerViewImpl02 impl02 = new PopupRecyclerViewImpl02( v.getContext() );
                impl02.show( v, items );
            }
        } );

        findViewById( R.id.recyclerPop7 ).setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                new PopupRecyclerEmpty( v.getContext() ).show( v );
            }
        } );
    }

    private List< TypeViewHolder > convert( int count ) {

        List< TypeViewHolder > result = new ArrayList<>();
        for ( int i = 0; i < count; i++ ) {
            result.add( build() );
        }
        return result;
    }

    private TypeViewHolder build() {

        return new BaseViewHolder( R.layout.popup_item ) {

            @Override
            protected void onBind( ViewHolder viewHolder, View root, int position, int type, List< Object > payloads ) {

                root.setOnClickListener( new OnClickListener() {
                    @Override
                    public void onClick( View v ) {

                        /*mWindowFactory.dismiss();*/
                    }
                } );
            }
        };
    }
}